import proFetch from '../../../../hrpub/common/utils/project-fetch';
import {promptBox} from 'nc-lightapp-front';
import { getAppPageConfig } from '../../../../hrpub/common/utils/utils';

export default class MainAction{
    constructor(comp){
        this.comp = comp
        // comp.props.use.table('handitemgrid');
        if(comp.props.use){
            comp.props.use.form('handitemcard');
            comp.props.use.editTable('handitemgrid');
        }
       
    }

    // appConfig = {
    //     pagecode : '60091040nccloud',
    //     appcode : '60091040'
    // }

    //获取模版
    getTemplate = ()=> {
        const {props} = this.comp
        const {createUIDom,button,meta} = props

        createUIDom(this.appConfig,(data)=>{
            this.getMulitiLange()
            meta.setMeta(data.template || [])
            button.setButtons(data.button || [])
        })
    }

    //获取多语
    getMulitiLange = () =>{
        const {props} = this.comp
        const {MultiInit,dispatch} = props

        MultiInit.getMultiLang({
            moduleId: 'gx6008',
            domainName: 'hrtrn',
            callback: (json,status,init) =>{
                dispatch({
                    type: 'main/update',
                    payload: {
                        json : json
                    } 
                })
                this.initMeta(props.meta.getMeta())
            }
        })
    }

    initPageData = () =>{
        const {props} = this.comp
        props.dispatch({
            type: 'main/getTreeData',
            payload :''
        }).then((res)=>{
            if(res.data){
                this.getTreeData(res.data,props)
            }
        })
    }

    //页面初始按钮状态
    initButtonStatus =()=>{
        const {props} = this.comp
        props.button.setButtonDisabled({
            'add': true,
            'delete': true
        })
    }

    //操作模版
    initMeta =(template)=>{
        const {props} = this.comp
        const {main} = props
        let items = template['handitemgrid'].items
        items.push({
            attrcode: 'opr',
            itemtype: 'customer',
            label: main.json['gx6008-000046'], /* 国际化处理： 操作*/
            width: '200px',
            textAlign: 'center',
            visible: true,
            fixed: 'right',
            render(text, record, index) {
                return(
                    <span>
                        <a style={{ cursor: 'pointer'}}
                            onClick={() => {
                                proFetch({
                                    url: '/nccloud/hrtrn/handitem/HandItemEditAction.do',
                                    body: {
                                        pk_handitem: record.values.pk_handitem.value
                                    }
                                }).then((res)=>{
                                    props.form.setAllFormValue({'handitemcard':res.data && res.data.handitemcard})
                                    props.form.setFormStatus('handitemcard','edit')
                                    props.dispatch({
                                        type: 'main/update',
                                        payload:{
                                            operation : 'edit',
                                            modaltitle : main.json['gx6008-000038']
                                        }
                                    })
                                    props.modal.show('addModal')
                                })  
                            }}
                        >{main.json['gx6008-000038']}</a>{/* 国际化处理： 修改*/}
                        <a style={{ cursor: 'pointer',paddingLeft:20}}
                            onClick={() => {
                                promptBox(
                                    {
                                        color: "warning",
                                        title: main.json['gx6008-000018'], /* 国际化处理： 提示*/
                                        content: main.json['gx6008-000019'], /* 国际化处理： 确定删除所选数据*/
                                        beSureBtnClick: function () {
                                            props.dispatch({
                                                type: 'main/delTableData',
                                                payload: {
                                                    pks :  record.values.pk_handitem.value
                                                }
                                            }).then((res)=>{
                                               props.editTable.deleteTableRowsByIndex('handitemgrid',index,true)
                                            })
                                        }
                                    }
                                )
                                
                            }}
                        >{main.json['gx6008-000039']}</a>{/* 国际化处理： 删除*/}
                    </span>
                )
            }
        })
        return template
    }

    //处理后台传回的的左树数据，使之符合hrtree组件的数据格式
    getTreeData =(data,props) =>{
        let treeData = []
        let leaveChildren = []
        let entryChildren = []
        if(data){
            if(data.entryjob){
                data.entryjob.forEach((item)=>{
                    if(item.key === 'entryjob'){
                        treeData.push(item)
                    }else{
                        item.pid = 'entryjob'
                        entryChildren.push(item)
                    }
                })
                if(treeData[0].children.length === 0){
                    treeData[0].children = entryChildren
                }
            }
            if(data.leavejob){
                 data.leavejob.forEach((item) => {
                    if(item.key === 'leavejob'){
                        treeData.push(item)
                    }else{
                        item.pid = 'leavejob'
                        leaveChildren.push(item)
                    }
                })
                if( treeData[1].children.length === 0){
                    treeData[1].children = leaveChildren
                }
            }
        }
        props.dispatch({
            type : 'main/update',
            payload : {
                treeData : treeData
            }
        })

    }

    didMount =() =>{
        const {props} = this.comp
        if (window.location.href.match('localhost:3006'))  window.location.hash = '#/ifr?page=201981519'
            // window.location.hash = '#?&c=60091040&p=60091040nccloud'
        this.appConfig = getAppPageConfig()
        let height = document.documentElement.clientHeight;
        props.dispatch({
            type: 'main/update',
            payload: {
                pageHeight :  height - 175
            }
        })
        this.getTemplate()
        this.initButtonStatus()
        this.initPageData()
        window.hh = this
    }

}